<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="爬取分类" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择分类" clearable size="small">
          <el-option label="全部" value="" />
          <el-option
              v-for="dict in crawlingTyps"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
          />
          </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="demandName">
        <el-input
          v-model="queryParams.demandName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布人" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入发布人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布日期">
        <el-date-picker
          v-model="daterangePublishTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="开始日期" prop="starttime">
        <el-date-picker clearable size="small"
          v-model="queryParams.starttime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endtime">
        <el-date-picker clearable size="small"
          v-model="queryParams.endtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="采购单位" prop="buyingUnit">
        <el-input
          v-model="queryParams.buyingUnit"
          placeholder="请输入采购单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:crawling:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-document"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:crawling:edit']"
        >查看详情</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:crawling:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crm:crawling:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="crawlingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="需求/项目名称" align="center" prop="demandName"  width="150">
        <template slot-scope="scope">
          <el-link :href="scope.row.url" style="color:#1890ff" target="_blank">{{scope.row.demandName}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="类型" :formatter="crawlingTypsFormat" align="center" prop="type" />
      <el-table-column label="发布人" align="center" prop="publisher" />
      <el-table-column label="发布时间" align="center" prop="publishTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="概述" align="center" prop="summary"  width="220" show-overflow-tooltip/>
      <el-table-column label="预算(万元)" align="center" prop="demandBudget"  width="60"/>
      <el-table-column label="开始-结束日期" align="center" prop="starttime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.starttime, '{y}-{m}-{d}') }}-{{ parseTime(scope.row.endtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计采购时间" align="center" prop="estimateTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.estimateTime, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="采购单位" align="center" prop="buyingUnit" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="联系人" align="center" prop="contacts"  width="80"/>
      <el-table-column label="联系电话" align="center" prop="contactsTel" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.annex != null"
            icon="el-icon-download"
            @click="downloadFile(scope.row.annex)"
          >附件</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:crawling:edit']"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:crawling:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据抓取对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" disabled="true">
            <el-option label="全部" value="" />
            <el-option
                v-for="dict in crawlingTyps"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="需求/项目名称" prop="demandName">
          <el-input v-model="form.demandName" placeholder="请输入需求名称"  readonly="true"/>
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="发布人" prop="publisher">
              <el-input v-model="form.publisher" placeholder="请输入发布人"  readonly="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发布时间" prop="publishTime">
              <el-date-picker clearable size="small"
                v-model="form.publishTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择发布时间" readonly="true">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预算金额（万元）" prop="demandBudget" v-if="form.demandBudget != null">
              <el-input v-model="form.demandBudget" placeholder="请输入内容" readonly="true" style="float:right;"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="概述" prop="summary">
          <el-input v-model="form.summary" type="textarea" placeholder="请输入内容" readonly="true"/>
        </el-form-item>
        <el-form-item label="预计采购时间" prop="estimateTime" v-if="form.estimateTime != null">
              <el-date-picker clearable size="small"
                v-model="form.estimateTime"
                type="month"
                value-format="yyyy-MM"
                placeholder="选择预计采购日期" readonly="true">
              </el-date-picker>
            </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始日期" prop="starttime" v-if="form.starttime != null">
              <el-date-picker clearable size="small"
                v-model="form.starttime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择开始日期" readonly="true">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="endtime" v-if="form.endtime != null">
              <el-date-picker clearable size="small"
                v-model="form.endtime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择结束日期" readonly="true">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>        
        
        <el-form-item label="需求内容">
            <div v-html="form.demandContent"></div>
        </el-form-item>
        <el-form-item label="采购单位" prop="buyingUnit">
          <el-input v-model="form.buyingUnit" placeholder="请输入采购单位" readonly="true"/>
        </el-form-item>
        <el-form-item label="地址" prop="address" v-if="form.address != null">
          <el-input v-model="form.address" placeholder="请输入地址" readonly="true"/>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contacts" v-if="form.contacts != null">
              <el-input v-model="form.contacts" placeholder="请输入联系人" readonly="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactsTel" v-if="form.contactsTel != null">
              <el-input v-model="form.contactsTel" placeholder="请输入联系电话" readonly="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="代理机构" prop="agency" v-if="form.agency != null">
          <el-input v-model="form.agency" placeholder="请输入代理机构" readonly="true"/>
        </el-form-item>
        <el-form-item label="地址" prop="agencyAddress" v-if="form.agencyAddress != null">
          <el-input v-model="form.agencyAddress" placeholder="请输入代理机构地址" readonly="true"/>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="agencyContacts" v-if="form.agencyContacts != null">
              <el-input v-model="form.agencyContacts" placeholder="请输入代理机构联系人" readonly="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="agencyContactsTel" v-if="form.agencyContactsTel != null">
              <el-input v-model="form.agencyContactsTel" placeholder="请输入代理机构联系人联系方式" readonly="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="附件" prop="annex" v-if="form.annex != null">
          <el-link :href="form.annex" style="color:#1890ff" target="_blank">附件</el-link>
        </el-form-item>
        <el-form-item label="原文地址" prop="url" v-if="form.url != null">
          <el-link :href="form.url" style="color:#1890ff" target="_blank">{{form.url}}</el-link>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCrawling, getCrawling, delCrawling, addCrawling, updateCrawling, exportCrawling } from "@/api/crm/crawling/crawling";
import Editor from '@/components/Editor';

export default {
  name: "Crawling",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据抓取表格数据
      crawlingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      crawlingTyps: [],
      // 发布时间时间范围
      daterangePublishTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        demandName: null,
        publisher: null,
        publishTime: null,
        summary: null,
        starttime: null,
        endtime: null,
        buyingUnit: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("crawling_type").then(response => {
      this.crawlingTyps = response.data;
    });
  },
  methods: {
    crawlingTypsFormat(row, column){
      return this.selectDictLabel(this.crawlingTyps, row.type);
    },
    downloadFile(url){
      window.open(url);
    },
    /** 查询数据抓取列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangePublishTime && '' != this.daterangePublishTime) {
        this.queryParams.params["beginPublishTime"] = this.daterangePublishTime[0];
        this.queryParams.params["endPublishTime"] = this.daterangePublishTime[1];
      }
      listCrawling(this.queryParams).then(response => {
        this.crawlingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        demandName: null,
        publisher: null,
        publishTime: null,
        summary: null,
        starttime: null,
        endtime: null,
        demandContent: null,
        demandBudget: null,
        buyingUnit: null,
        address: null,
        contacts: null,
        contactsTel: null,
        agency: null,
        agencyAddress: null,
        agencyContacts: null,
        agencyContactsTel: null,
        annex: null,
        url: null,
        region: null,
        valid: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      //this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePublishTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据抓取";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCrawling(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "意向/需求详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCrawling(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCrawling(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除数据抓取编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCrawling(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据抓取数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCrawling(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
